//
//  LKWaitHandleCustomerViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/14.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKWaitHandleCustomerViewController.h"
#import "LKWorkCustomListCell.h"
#import "LKConfirmCustomInforViewController.h"
#import "LKCustomerModel.h"
#import "LKWorkCustomerDetailViewController.h"
#import "LKCustomerAppointmentViewController.h"
#import "LKAddWorkCustomViewController.h"
@interface LKWaitHandleCustomerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSMutableArray *dataListArray;
@property(nonatomic,assign)NSInteger currentPageIndex;
@property(nonatomic,assign)NSInteger MaxResultCount;
@end

@implementation LKWaitHandleCustomerViewController
-(LKBaseTableview *)tableView{
    if (!_tableView) {
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT + 12, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 12)) style:UITableViewStyleGrouped registercellClasss:@[@"LKWorkCustomListCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
    }
    return _tableView;
}
/**
 头部下拉刷新
 */
-(void)headerReloadData{
    self.currentPageIndex = 1;
    [self getDataList];
}

/**
 尾部上拉加载更多
 */
-(void)footerReloadData{
    self.currentPageIndex++;
    [self getDataList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 200;
    self.currentPageIndex = 1;
    self.MaxResultCount = 10;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self getDataList];
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    [super setNavgationBackBtn];
    self.navigationItem.title = @"待处理客户";
//    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [btn setImage:[UIImage imageNamed:@"testpagesousuo"] forState:UIControlStateNormal];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataListArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf)
    LKWorkCustomListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWorkCustomListCell"];
     cell.model = self.dataListArray[indexPath.section];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(customerDetailInforamtion:)];
    [cell.labelsView.tableview addGestureRecognizer:tap];
    
    cell.labelsView.tableview.tag = 1000000 + indexPath.section;
    cell.appointmentBtn.tag = 300000+indexPath.section;
    [cell.appointmentBtn addTarget:self action:@selector(appointCustomerInformation:) forControlEvents:UIControlEventTouchUpInside];
    cell.updateBtn.tag = 200000 + indexPath.section;
    [cell.updateBtn addTarget:self action:@selector(updateCustomerInformation:) forControlEvents:UIControlEventTouchUpInside];
    [cell.deletBtn addTarget:self action:@selector(deletCustomerACtion:) forControlEvents:UIControlEventTouchUpInside];
    cell.orderBtn.tag = 400000 + indexPath.section;
    [cell.orderBtn addTarget:self action:@selector(addOrderInformation:) forControlEvents:UIControlEventTouchUpInside];
    [[cell.foundOrderBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
    }];
    cell.deletBtn.tag = 100000 + indexPath.section;
    return cell;
}
-(void)customerDetailInforamtion:(UITapGestureRecognizer *)tap{
    UIView *view = tap.view;
    LKCustomerModel *model = self.dataListArray[view.tag - 1000000];
    LKWorkCustomerDetailViewController *vcs = [[LKWorkCustomerDetailViewController alloc] init];
    vcs.model = model;
    [self.navigationController pushViewController:vcs animated:YES];
}
/**
 添加订单
 */
-(void)addOrderInformation:(UIButton *)btn{
    LKCustomerModel *model = self.dataListArray[btn.tag - 400000];
    LKConfirmCustomInforViewController *vc = [[LKConfirmCustomInforViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
/**
 预约产品信息
 
 */
-(void)appointCustomerInformation:(UIButton *)btn{
    LKCustomerModel *model = self.dataListArray[btn.tag - 300000];
    LKCustomerAppointmentViewController *vc = [[LKCustomerAppointmentViewController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
/**
 更新客户信息
 */
-(void)updateCustomerInformation:(UIButton *)btn{
    LKCustomerModel *model = self.dataListArray[btn.tag - 200000];
    LKAddWorkCustomViewController *vc = [[LKAddWorkCustomViewController alloc] init];
    vc.updateModel = model;
    vc.updateType = YES;
    vc.headerTitle = btn.titleLabel.text;
    [self.navigationController pushViewController:vc animated:YES];
}

/**
 删除客户信息
 */
-(void)deletCustomerACtion:(UIButton *)btn{
    LKCustomerModel *model = self.dataListArray[btn.tag - 100000];
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认要删除?" message:model.customerName preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self deleteCustomerWithModel:model];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
/**
 删除客户
 */
-(void)deleteCustomerWithModel:(LKCustomerModel *)model{
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:deleteCutomerUrl header:[LKUserInforTool header] parameters:@{@"Id":model.customerId} progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        
        [self.dataListArray removeObject:model];
        [self.tableView reloadData];
//        [self getAccountDetailInformation];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 248;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 13;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
/**
 获取数据列表
 */
-(void)getDataList{
    NSString *PageIndexStr = [NSString stringWithFormat:@"%ld",self.currentPageIndex];
    NSString *MaxResultCountStr = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    NSDictionary *param = @{@"PageIndex":PageIndexStr,@"MaxResultCount":MaxResultCountStr};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:waitHandCustomerUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.currentPageIndex == 1) {
            [self.dataListArray removeAllObjects];
        }
        NSArray *dataArra = responseObject[@"result"][@"items"];
        for (NSDictionary *dic in dataArra) {
            LKCustomerModel *model = [LKCustomerModel modelWithDictionary:dic];
            [self.dataListArray addObject:model];
        }
//        if (dataArra.count < self.dataListArray.count) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        if (self.dataListArray.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(NSMutableArray *)dataListArray{
    if (!_dataListArray) {
        _dataListArray = [[NSMutableArray alloc] init];
    }
    return _dataListArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
